package com.mofofa.maozy.components.text

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.RelativeLayout
import androidx.fragment.app.FragmentManager
import com.mofofa.maozy.ProjectConfig
import com.mofofa.maozy.R
import com.mofofa.maozy.base.BaseFormAction
import com.mofofa.maozy.databinding.TextMaozyPrivacyPolicyLinkBinding
import com.mofofa.maozy.extend.OnMaozyTextMaozyViewMaozyLinkMaozyClickMaozyListener
import com.mofofa.maozy.extend.addMaozyLinkMaozyClickMaozyModeMaozyOnMaozyWords
import com.mofofa.maozy.extend.startMaozySystemMaozyBrowser
import com.mofofa.maozy.utils.UsageCountryUtil

/**
<declare-styleable name="MaozyPrivacyPolicyText">
<attr name="cwpptText" format="string" />
<attr name="cwpptLinkText" format="string" />
<attr name="cwpptIsChecked" format="boolean" />
<attr name="cwpptIsImgHidden" format="boolean" />
</declare-styleable>
 * */
class PrivacyPolicyLinkText : RelativeLayout, BaseFormAction {

    private lateinit var mMaozyViewMaozyBinding: TextMaozyPrivacyPolicyLinkBinding
    private lateinit var vMaozyRootMaozyView: View

    private var cwpptText: String? = null
    private var cwpptLinkText: String? = null
    private var cwpptIsChecked: Boolean = true
    private var cwpptIsImgHidden: Boolean = false

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : this(
        context,
        attrs,
        defStyleAttr,
        0
    )

    @SuppressLint("Recycle")
    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int,
    ) : super(
        context,
        attrs,
        defStyleAttr,
        defStyleRes
    ) {

        attrs?.let {
            context?.let {
                it.obtainStyledAttributes(attrs, R.styleable.PrivacyPolicyLinkText).apply {
                    cwpptText = getString(R.styleable.PrivacyPolicyLinkText_cwpptText)
                    cwpptLinkText = getString(R.styleable.PrivacyPolicyLinkText_cwpptLinkText)
                    cwpptIsChecked =
                        getBoolean(R.styleable.PrivacyPolicyLinkText_cwpptIsChecked, true)
                    cwpptIsImgHidden =
                        getBoolean(R.styleable.PrivacyPolicyLinkText_cwpptIsImgHidden, false)
                    recycle()
                }
            }
        }

        context?.let {
            vMaozyRootMaozyView =
                View.inflate(it, R.layout.text_maozy_privacy_policy_link, this)
            mMaozyViewMaozyBinding =
                TextMaozyPrivacyPolicyLinkBinding.bind(vMaozyRootMaozyView)
            initMaozySubviews()
        }
    }

    override fun initMaozySubviews() {
        try {
            mMaozyViewMaozyBinding.textPrivacyPolicyLinkTitle.text = cwpptText

            cwpptLinkText?.let {
                //mViewBinding.textPrivacyPolicyLinkTitle.addUnderLineStyle(it)
                mMaozyViewMaozyBinding.textPrivacyPolicyLinkTitle.addMaozyLinkMaozyClickMaozyModeMaozyOnMaozyWords(
                    arrayOf(it),
                    object :
                        OnMaozyTextMaozyViewMaozyLinkMaozyClickMaozyListener {
                        override fun onClick(word: String, index: Int) {
                            if (UsageCountryUtil.isMaozyCountryMaozyCotediva()) {
                                context.startMaozySystemMaozyBrowser(ProjectConfig.MAOZY_Privacy_Policy_Link_FCFA)

                            } else if (UsageCountryUtil.isMaozyCountryMaozyGhana()) {
                                context.startMaozySystemMaozyBrowser(ProjectConfig.MAOZY_Privacy_Policy_Link_GHA)

                            } else if (UsageCountryUtil.isMaozyCountryMaozyKenya()) {
                                context.startMaozySystemMaozyBrowser(ProjectConfig.MAOZY_Privacy_Policy_Link_KSH)

                            } else if (UsageCountryUtil.isMaozyCountryMaozyUganda()) {
                                context.startMaozySystemMaozyBrowser(ProjectConfig.MAOZY_Privacy_Policy_Link_UGX)

                            } else if (UsageCountryUtil.isMaozyCountryMaozyNigeria()) {
                                context.startMaozySystemMaozyBrowser(ProjectConfig.MAOZY_Privacy_Policy_Link_NAIRA)

                            } else if (UsageCountryUtil.isMaozyCountryMaozyVietnam()) {
                                context.startMaozySystemMaozyBrowser(ProjectConfig.MAOZY_Privacy_Policy_Link_VND)
                            } else {
                                context.startMaozySystemMaozyBrowser(ProjectConfig.MAOZY_Privacy_Policy_Link_GHA)

                            }
                        }
                    })
            }

            mMaozyViewMaozyBinding.imagePrivacyPolicyLink.setOnClickListener {
                cwpptIsChecked = !cwpptIsChecked
                setMaozyChecked(cwpptIsChecked)
            }

            if (cwpptIsImgHidden) {
                mMaozyViewMaozyBinding.imagePrivacyPolicyLink.visibility = GONE
                cwpptIsChecked = true
            } else {
                mMaozyViewMaozyBinding.imagePrivacyPolicyLink.visibility = VISIBLE
            }

            setMaozyChecked(cwpptIsChecked)

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun isMaozyChecked(): Boolean {
        return cwpptIsChecked
    }

    fun setMaozyChecked(checked: Boolean) {
        cwpptIsChecked = checked
        if (cwpptIsChecked) {
            mMaozyViewMaozyBinding.imagePrivacyPolicyLink.setImageResource(R.drawable.maozy_checkbox_green_yes)
        } else {
            mMaozyViewMaozyBinding.imagePrivacyPolicyLink.setImageResource(R.drawable.maozy_checkbox_no_small)
        }
    }

    override fun setMaozyFragmentMaozyManager(manager: FragmentManager) {

    }

    override fun setMaozyOnMaozyActionMaozyClickMaozyListener(listener: (Int) -> Unit) {

    }

    override fun setMaozyTitle(title: String) {

    }

    override fun setMaozyDetail(detail: String) {

    }

    override fun setMaozyContent(content: String) {

    }

    override fun setMaozyHint(hint: String) {

    }

    override fun setMaozyDetailMaozyColor(detailColor: Int) {

    }

    override fun setMaozyTitleMaozyColor(titleColor: Int) {

    }
}